//
//  BVModifyAddressViewController.m
//  BuyVegetable
//
//  Created by yushanchang on 16/10/21.
//  Copyright © 2016年 com.yinding. All rights reserved.
//

#import "BVModifyAddressViewController.h"
#import "KLCPopup.h"
#import "BVMineTool.h"
#import "BVAllAddress.h"
#import "BVAllCityTool.h"
#import "BVTextView.h"
#import "BVIndexTool.h"
#import "BVSelectArea.h"

@interface BVModifyAddressViewController ()<UIPickerViewDataSource, UIPickerViewDelegate,UITextViewDelegate>

@property (weak, nonatomic) IBOutlet UIButton *selectAddress;
@property (weak, nonatomic) IBOutlet UITextView *detailTextView;
@property (strong, nonatomic) IBOutlet UIView *pickerBgView;
@property (weak, nonatomic) IBOutlet UIPickerView *pickerView;
@property (nonatomic, strong) KLCPopup *popUp;
@property (nonatomic, strong) BVIndexTool *indexTool;
@property (nonatomic, strong) NSArray *provArray;
@property (nonatomic, copy) NSString *buttonTitle;
@property (nonatomic, copy) NSString *provId;
@property (nonatomic, copy) NSString *cityId;

@end

@implementation BVModifyAddressViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.navigationItem setTitle:@"我的地址"];
    [self setupRightBarButtonItem];
    [self.navItem setEnabled:NO];
    [self.navItem setTitleColor:[UIColor BVLineGrayColor] forState:UIControlStateNormal];
    [self fetchCityData];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.selectAddress setTitle:self.user.address forState:UIControlStateNormal];
    self.detailTextView.text = self.user.detail_address;
}

- (void)fetchCityData {
    [self.indexTool fetchAllProvinceSuccess:^(NSArray *provinces) {
        self.provArray = provinces;
    } failure:^(NSError *error) {
        
    }];
}
#pragma mark -
#pragma mark - save
- (void)confirmSaveClick {
    NSInteger index = [self.pickerView selectedRowInComponent:0];
    NSInteger index2 = [self.pickerView selectedRowInComponent:1];
    
    NSString *provId = [NSString stringWithFormat:@"%@",[self.provArray[index] ID]];
    NSString *cityId = [NSString stringWithFormat:@"%@",[[self.provArray[index] cities][index2] ID]];
    /*容错处理*/
    if ([provId isEqualToString:@""] || [cityId isEqualToString:@""] || provId == nil || cityId == nil || [self.provId isEqualToString:@""] || [self.cityId isEqualToString:@""]) {
        self.provId = self.user.addr_provinceId;
        self.cityId = self.user.addr_cityId;
    } else {
        self.provId = provId;
        self.cityId = cityId;
    }
//    NSLog(@"proId:%@-city:Id%@",provId,cityId);
    [self.hud show:YES];
    [JDStatusBarNotification showWithStatus:@"正在保存" styleName:JDStatusBarStyleDefault ];
    [self.mineTool modifyUserAddress:self.buttonTitle detailAddr:self.detailTextView.text provId:self.provId cityId:self.cityId success:^{
        [self.hud hide:YES];
        [JDStatusBarNotification showWithStatus:@"保存成功" dismissAfter:2.0 styleName:JDStatusBarStyleSuccess];
        [self.navigationController popViewControllerAnimated:YES];
    } failure:^(NSError *error) {
        [self.hud hide:YES];
        [self showError:@"保存失败"];
    }];
}

- (IBAction)cancelClick:(id)sender {
    self.provId = @"";
    self.cityId = @"";
    [self.popUp dismiss:YES];
}

- (void)textViewDidChange:(UITextView *)textView {
    if ([self.user.detail_address isEqualToString:textView.text]) {
        [self.navItem setEnabled:NO];
        [self.navItem setTitleColor:[UIColor BVLineGrayColor] forState:UIControlStateNormal];
    } else {
        [self.navItem setEnabled:YES];
        [self.navItem setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    }
}

- (IBAction)confirmClick:(id)sender {
    
    NSInteger index = [self.pickerView selectedRowInComponent:0];
    NSInteger index2 = [self.pickerView selectedRowInComponent:1];
    
    NSString *provinceTitle = [NSString stringWithFormat:@"%@",[self.provArray[index] names]];
    NSString *cityTitle = [NSString stringWithFormat:@"%@",[[self.provArray[index] cities][index2] names]];
    
    NSString *buttonTitle = [NSString stringWithFormat:@"%@-%@",provinceTitle,cityTitle];
    self.buttonTitle = buttonTitle;
    [self.selectAddress setTitle:buttonTitle forState:UIControlStateNormal];
    
    if ([self.buttonTitle isEqualToString:self.user.address]) {
        [self.navItem setEnabled:NO];
        [self.navItem setTitleColor:[UIColor BVLineGrayColor] forState:UIControlStateNormal];
    } else {
        [self.navItem setEnabled:YES];
        [self.navItem setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    }
    
    [self.popUp dismiss:YES];
    [self.detailTextView resignFirstResponder];
}

#pragma mark - 
#pragma mark - UIPickerViewDataSource, UIPickerViewDelegate>
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    return 2;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    
    if (component == 0) {
        return self.provArray.count;
    } else {
        NSInteger index = [pickerView selectedRowInComponent:0];
        return [self.provArray[index] cities].count;
    }
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {

    if (component == 0) {
        return [self.provArray[row] names];
    } else {
        NSInteger index = [pickerView selectedRowInComponent:0];
        return [[self.provArray[index] cities][row] names];
    }
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    
    if (component == 0) {
        [pickerView reloadComponent:1];
    } else {
        [pickerView reloadComponent:0];
    }
}

#pragma mark -
#pragma mark - 地址按钮点击事件
- (IBAction)selectAddressClick:(id)sender {
    self.popUp = [KLCPopup popupWithContentView:self.pickerBgView];
    self.popUp.shouldDismissOnBackgroundTouch = NO;
    self.popUp.maskType = KLCPopupMaskTypeNone;
    [self.popUp show];
}

- (void)setPickerBgView:(UIView *)pickerBgView {
    _pickerBgView = pickerBgView;
    [pickerBgView setFrame:CGRectMake(0, 0, ScreenW, 250)];
}

- (BVIndexTool *)indexTool {
    if (!_indexTool) {
        _indexTool = [[BVIndexTool alloc] init];
    }
    return _indexTool;
}

- (IBAction)tap:(id)sender {
    [self.view endEditing:YES];
}


@end
